import { apiConstants,loadModalInfo,loadConfirmModal,manageServer} from '@/js/utils'

new Vue({
  el: "#app",
  data() {
    return {
      defaultRoleAlias:{},
      defaultDepartMentAlias:{},
      companyInfo:{
        name:"",
        contactName:"",
        contactPhone:"",
        type:"1", //0 试用 1正式
        status:"",//0 冻结 1 激活
        roleAlias:{},
        departMentAlias:{},
        createTime:"",
        endDate:"",
        useCount:0,
        username:"",
        password:"",

      },
      levelObj:{
        company:"一级",
        workshop:"二级",
        team:"三级",
        staff:"四级"
      },
      isSelectFormal:true,
      manageServer:"",
    }
  },
  created() {
    if(companyId){
      this.manageServer = manageServer+'/login?companyId='+companyId;
      this.getCompanyInfo();
    }
    this.getAlias();
  },
  methods: {
    getCompanyInfo(){
      let _this = this;
      _this.$ajax(apiConstants.company_getCompanyInfo, {
        data: {companyId:companyId},
        success: function (data) {
          _this.companyInfo = Object.assign({}, data.companyInfo);
          //产品服务
          let serviceIndex = data.serviceList.findIndex(val=>{return val.type==3});
          if(serviceIndex != -1){
            _this.companyInfo.useCount = data.serviceList[serviceIndex]["useCount"];
            _this.companyInfo.createTime = data.serviceList[serviceIndex]["createTime"];
            _this.companyInfo.endDate = data.serviceList[serviceIndex]["endTime"];
          }
          let adminIndex = data.adminList.findIndex(val=>{return val.product==3});
          if(adminIndex != -1){
            _this.companyInfo.username = data.adminList[adminIndex]["username"];
            _this.companyInfo.password = data.adminList[adminIndex]["password"];
          }
          _this.getAlias(companyId);
        }
      })
    },
    getLevel(key){
      return this.levelObj[key.replace("Role","")]
    },
    getAlias(id){
      let _this = this;
      _this.$ajax(apiConstants.company_getAlias, {
        data: {companyId:id},
        success: function (data) {
          if(data){
            if(!id){
              _this.defaultRoleAlias = data.roleAlias;
              _this.defaultDepartMentAlias = data.departMentAlias;
            }
            _this.companyInfo.roleAlias = JSON.parse(JSON.stringify(data.roleAlias));
            _this.companyInfo.departMentAlias = JSON.parse(JSON.stringify(data.departMentAlias));
          }
        }
      })
    },
    changeStatus(){
      this.isSelectFormal = !this.isSelectFormal;
    },
    resetAlias(type){
      if(type == 1){
        this.companyInfo.departMentAlias = JSON.parse(JSON.stringify(this.defaultDepartMentAlias));
      }
      if(type == 2){
        this.companyInfo.roleAlias = JSON.parse(JSON.stringify(this.defaultRoleAlias));
      }
    },
    cancel(){
      location.href = "/index";
    },
    copyAddress(){
      let _this = this;
      let clipboard_1 = new Clipboard('#copyBtn');
      clipboard_1.on('success', function (e) {
        _this.$Message({
          content: "地址已复制到剪切板！",
          time: 1500,
          type: "default",
          hasClose: true
        });
      })
    },
    save(){
      let _this = this;
      if (!this.companyInfo.name) {
        loadModalInfo("请填写企业名称");
        return;
      }
      if (!this.companyInfo.contactName) {
        loadModalInfo("请填写企业联系人姓名");
        return;
      }
      if (!this.companyInfo.contactPhone) {
        loadModalInfo("请填写企业联系人电话");
        return;
      }
      let phoneReg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;

      if(!phoneReg.test(this.companyInfo.contactPhone)){
        loadModalInfo("请填写正确的企业联系人电话");
        return;
      }

      for(let key in this.companyInfo.departMentAlias){
        if(!this.companyInfo.departMentAlias[key]){
          loadModalInfo("请填写组织架构名称");
          return;
        }
      }
      for(let key in this.companyInfo.roleAlias){
        if(!this.companyInfo.roleAlias[key]["name"]){
          loadModalInfo("请填写角色名称");
          return;
        }
      }

      let obj = Object.assign({}, this.companyInfo);
      obj.id = companyId;
      obj.agentId = agentId;
      obj.type = this.isSelectFormal?1:0;
      obj.roleAlias = JSON.stringify(this.companyInfo.roleAlias);
      obj.departMentAlias = JSON.stringify(this.companyInfo.departMentAlias);

      loadConfirmModal("确认保存吗", () => {
        _this.$ajax(apiConstants.company_saveCompany, {
          data:obj,
          success: function (data) {
            setTimeout(function () {
              loadModalInfo(companyId?'修改':'新增'+"企业成功","/images/login/icon-chenggong.png");
              location.href="/index"
            },500)
          },
          fail: function (err) {
            loadModalInfo(err.message);
          }
        })
      },  ()=>{}, "确定");

    }
  },
});
